/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package page;

import dbase.DBconnection;
import dbase.DBanggota;
import dbase.DBkendaraan;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import model.anggota;
import model.kendaraan;
import javax.servlet.http.HttpSession;

/**
 *
 * @author luwis
 */
public class GoLihat {
    HttpServletRequest request;

    public GoLihat(HttpServletRequest request){
        this.request = request;
    }

    public String getPage(){
        //String catid = request.getParameter("catid");
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            DBconnection dbConn = new DBconnection();
            List<anggota> ang = new ArrayList<anggota>();
            DBanggota dbAng = new DBanggota(dbConn.getConnection());
        
            try{
                ang = dbAng.getAllAnggota();
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }

            request.setAttribute("dataanggota", ang);
            return "lihatPengendara.jsp";
        }
            else{
        return "index.jsp";
        }
    }
    public String getPageListApprove(){
        //String catid = request.getParameter("catid");
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            DBconnection dbConn = new DBconnection();
            List<anggota> ang = new ArrayList<anggota>();
            DBanggota dbAng = new DBanggota(dbConn.getConnection());

            try{
                ang = dbAng.getAllAnggotaApprove();
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }

            request.setAttribute("dataanggota", ang);
            return "daftarAprrove.jsp";
        }
            else{
        return "index.jsp";
        }
    }

public String getPage2(){
        //String catid = request.getParameter("catid");
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            DBconnection dbConn = new DBconnection();
            List<kendaraan> kend = new ArrayList<kendaraan>();
            DBkendaraan dbKend = new DBkendaraan(dbConn.getConnection());

            try{
                kend = dbKend.getAllKendaraan();
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }
            request.setAttribute("datakendaraan", kend);
            return "lihatKendaraanAll.jsp";
    }
        else{
            return "index.jsp";
        }
}


public String getPageKendaraan(){
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            String nrp = request.getParameter("nrp");
            //int id = Integer.parseInt(nrp);
            DBconnection dbConn = new DBconnection();
            List<kendaraan> kend = new ArrayList<kendaraan>();
            DBkendaraan dbKend = new DBkendaraan(dbConn.getConnection());

            try{
                kend = dbKend.getKendaraanByCategory(nrp);
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }
            request.setAttribute("datakendaraan", kend);
            return "lihatKendaraan.jsp";
    }
        else{
            return "index.jsp";
        }
}
public String getPageKendaraanEdit(){

        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            String nrp = request.getParameter("nrp");
            //int id = Integer.parseInt(nrp);
            DBconnection dbConn = new DBconnection();
            List<kendaraan> kend = new ArrayList<kendaraan>();
            DBkendaraan dbKend = new DBkendaraan(dbConn.getConnection());

            try{
                kend = dbKend.getKendaraanByCategory(nrp);
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                dbConn.closeConnection();
            }
            request.setAttribute("datakendaraan", kend);
            return "editAnggota.jsp";
        }
        else{
            return "index.jsp";
        }
}

public String deleteAnggota() {

        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            String nrp = request.getParameter("nrp");
            List<anggota> anggota1 = new ArrayList<anggota>();
            DBconnection dbConn = new DBconnection();
            DBanggota mdHap = new DBanggota(dbConn.getConnection());
            try {
                if (nrp == null) {
                    anggota1 = mdHap.getAllAnggota();
                    request.setAttribute("anggota", anggota1);
                } else {
                    int id = Integer.parseInt(nrp);
                    mdHap.deleteAnggota(id);
                    anggota1 = mdHap.getAllAnggota();
                    request.setAttribute("anggota", anggota1);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                dbConn.closeConnection();
            }
            return getPage();
    }
        else{
            return "index.jsp";   
            }
    }

}

